﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using WeiboPlus.Models;
using System.Threading;
using System.Windows.Media.Imaging;
using System.IO;
using System.Diagnostics;
using WeiboPlus.ViewModel;

namespace WeiboPlus.Shell.ViewModel
{
    public class SNSStatusViewModel : ViewModelBase<SNSStatus>, IComparable<SNSStatusViewModel>
    {
        double maxWidth;

        public double MaxWidth
        {
            get { return maxWidth; }
            set { maxWidth = value; RaisePropertyChanged(() => MaxWidth); }
        }

        double maxHeight;

        public double MaxHeight
        {
            get { return maxHeight; }
            set { maxHeight = value; RaisePropertyChanged(() => MaxHeight); }
        }

        public bool HasImage
        {
            get { return Image!=null; }
        }

        private Uri image;
        public Uri Image
        {
            get { return image; }
            set
            {
                image = value;
                RaisePropertyChanged(() => Image);
                RaisePropertyChanged(() => HasImage); 
            }
        }

        ImageSource _imageSource;
        public ImageSource ImageSource
        {
            get
            {
                return _imageSource;
            }
            set
            {
                if (_imageSource != value)
                {
                    _imageSource = value;
                    RaisePropertyChanged(() => ImageSource);
                }
            }
        }

        public bool HasRetweetImage
        {
            get { return RetweetImage != null; }
            set { RaisePropertyChanged(() => HasRetweetImage); }
        }

        private Uri retweetImage;
        public Uri RetweetImage
        {
            get { return retweetImage; }
            set
            {
                retweetImage = value;
                RaisePropertyChanged(() => RetweetImage);
            }
        }

        ImageSource _retweetImageSource;
        public ImageSource RetweetImageSource
        {
            get
            {
                return _retweetImageSource;
            }
            set
            {
                if (_retweetImageSource != value)
                {
                    _retweetImageSource = value;
                    RaisePropertyChanged(() => RetweetImageSource);
                }
            }
        }

        public SNSStatusViewModel()
        {
            this.PropertyChanged += (s, e) =>
            {
                if (e.PropertyName == "Item")
                {
                    if (Item != null)
                        Initialize();
                }
            };
        }

        protected override void Initialize()
        {
            InitImageUri();
            //base.Initialize();
            InitStatusInfoText();

            if (HasRetweet)
            {
                if (retweetStatus == null)
                    retweetStatus = new SNSStatusViewModel(Item.RetweetedStatus);
            }

            Item.PropertyChanged -= Item_PropertyChanged;
            Item.PropertyChanged += Item_PropertyChanged;

        }

        protected void InitStatusInfoText()
        {
            statusInfoText = string.Format("{0} 来自 {1}", Converter.TimeToStringConverter.GetFromNow(Item.CreatedTime), Item.Source);
            if (HasRetweet)
                RetweetedRepostCommentInfoText = string.Format("转发({0})评论({1})", Item.RetweetedStatus.RepostCount, Item.RetweetedStatus.CommentCount);
            RepostCommentInfoText = string.Format("转发({0})评论({1})", Item.RepostCount, Item.CommentCount);
            if (HasRetweet)
                retweetedStatusInfoText = string.Format("{0} 来自 {1}", Converter.TimeToStringConverter.GetFromNow(Item.RetweetedStatus.CreatedTime), Item.RetweetedStatus.Source);
        }

        void RetweetedStatus_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "CommentCount" || e.PropertyName == "RepostCount")
            {
                RetweetedRepostCommentInfoText = string.Format("转发({0})评论({1})", Item.RetweetedStatus.RepostCount, Item.RetweetedStatus.CommentCount);
            }
        }

        void Item_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "CommentCount" || e.PropertyName == "RepostCount")
            {
                RepostCommentInfoText = string.Format("转发({0})评论({1})", Item.RepostCount, Item.CommentCount);
            }
            //throw new NotImplementedException();
        }

        public SNSStatusViewModel(SNSStatus status)
            : base(status)
        {
            Initialize();
        }

        protected virtual void InitImageUri()
        {
            var iq = PhoneApplicationController.Current.AppSetting.ImageQuality;
            string pic = null;
            string repic = null;
            switch (iq)
            {
                case ImageQualityTypes.DisplayThumb:
                    pic = Item.ThumbnailPic;
                    if (Item.RetweetedStatus != null)
                        repic = Item.RetweetedStatus.ThumbnailPic;
                    MaxHeight = 200;
                    MaxWidth = 120;
                    break;
                case ImageQualityTypes.DisplayFull:
                    pic = Item.BmiddlePic;
                    if (Item.RetweetedStatus != null)
                        repic = Item.RetweetedStatus.BmiddlePic;
                    MaxHeight = 1800;
                    MaxWidth = 480;
                    break;
                case ImageQualityTypes.DisplayNone:
                default:
                    break;
            }
            if (!string.IsNullOrEmpty(pic))
            {
                if (pic.ToUpper().EndsWith(".GIF"))
                {
                    Image = new Uri("Images/gificon.png", UriKind.Relative);
                    MaxHeight = 50;
                    MaxWidth = 50;
                }
                else
                    Image = new Uri(pic, UriKind.Absolute);
                //Debug.WriteLine("Image:" + pic);
            }

            if (Item.RetweetedStatus != null && !string.IsNullOrEmpty(repic))
            {
                if (repic.ToUpper().EndsWith(".GIF"))
                {
                    RetweetImage = new Uri("Images/gificon.png", UriKind.Relative);
                    MaxHeight = 50;
                    MaxWidth = 50;
                }
                else
                    RetweetImage = new Uri(repic, UriKind.Absolute);
                //Debug.WriteLine("RetweetImage:" + repic);
            }
            if (Item.User != null)
            {
                if (!string.IsNullOrEmpty(Item.User.ProfileImageUrl) && Item.User.ProfileImageUrl.StartsWith("http://"))
                {
                    FaceImageUri = new Uri(Item.User.ProfileImageUrl, UriKind.Absolute);
                    //Debug.WriteLine("FaceImageUri:" + Item.User.ProfileImageUrl);
                }
                else
                {
                    FaceImageUri = new Uri("Images/SmallDefaultProfile.png", UriKind.Relative);
                }
            }
        }


        public bool IsReplay
        {
            get { return !string.IsNullOrEmpty(Item.ReplyToStatusId); }
        }

        public virtual bool HasRetweet
        {
            get
            {
                return Item.RetweetedStatus != null;
            }
        }

        SNSStatusViewModel retweetStatus;
        public SNSStatusViewModel RetweetStatus
        {
            get
            {
                return retweetStatus;
            }
        }

        ////private ImageSource userFaceImage;
        //protected WeakReference<ImageBrush> bitmapFaceImage;
        //public Uri UserFaceImage
        //{
        //    get
        //    {
        //        //if (bitmapFaceImage != null)
        //        //{
        //        //    if (bitmapFaceImage.Target != null)
        //        //    {
        //        //        return bitmapFaceImage.Target;
        //        //    }
        //        //}
        //        return new Uri(Item.User.ProfileImageUrl, UriKind.Absolute) ;
        //    }
        //}

        Uri faceImageUri;
        public Uri FaceImageUri
        {
            get { return faceImageUri; }
            set
            {
                if (value != faceImageUri)
                {
                    faceImageUri = value;
                    RaisePropertyChanged(() => FaceImageUri);
                }
            }
        }

        ImageSource _faceImageSource;
        public ImageSource FaceImageSource
        {
            get
            {
                return _faceImageSource;
            }
            set
            {
                if (_faceImageSource != value)
                {
                    _faceImageSource = value;
                    RaisePropertyChanged(() => FaceImageSource);
                }
            }
        }

        string statusInfoText;
        public string StatusInfoText
        {
            get
            {
                return statusInfoText;
            }
        }

        string repostCommentInfoText;
        public string RepostCommentInfoText
        {
            get
            {
                return repostCommentInfoText;
            }
            set
            {
                repostCommentInfoText = value;
                RaisePropertyChanged("RepostCommentInfoText");
            }
        }
        string retweetedRepostCommentInfoText;
        public string RetweetedRepostCommentInfoText
        {
            get
            {
                return retweetedRepostCommentInfoText;
            }
            set
            {
                retweetedRepostCommentInfoText = value;
                RaisePropertyChanged("RetweetedRepostCommentInfoText");
            }
        }

        string retweetedStatusInfoText;
        public string RetweetedStatusInfoText
        {
            get
            {
                return retweetedStatusInfoText;
            }
        }

        public int CompareTo(SNSStatusViewModel other)
        {
            return DateTime.Compare(Item.CreatedTime, other.Item.CreatedTime);
        }

    }
}
